package com.logger;

import java.io.File;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fmk.utils.RegExpUtils;
import com.fmk.utils.StringUtils;

/**
 * 
 * @author bbarhoumi
 * 
 */
public class LogFile extends FileName {
    private final String               userName;
    private String                     sessionId;
    private final Date                 date;
    private final Integer              connId;
    private final List<LogQuery>       lstLogQuery;
    private final List<LogTransaction> lstLogTransaction;
    {
        this.lstLogQuery = new ArrayList<LogQuery>();
        this.lstLogTransaction = new ArrayList<LogTransaction>();

    }

    public LogFile(final File file) {
        super(file);
        final String fileName = super.getFileName(false);
        this.userName = LogFile.getUserName(fileName);
        this.date = LogFile.getDate(fileName);
        this.connId = LogFile.getConnId(fileName);
    }

    public LogFile(final String pathname) {
        this(new File(pathname));
    }

    public static boolean isUserLogFile(final File file) {
        final String filename = file.getName();
        return LogFile.isUserLogFile(filename);
    }

    public static boolean isUserLogFile(final String filename) {
        final String _regularExp = RegExpUtils.Constants.REGX_USER_LOG_FILE;
        return RegExpUtils.isMatchFound(filename, _regularExp);
    }

    public String getUserName() {
        return this.userName;
    }

    public static String getUserName(final String fileName) {
        if (fileName == null) {
            return null;
        }
        final String[] split = fileName.split(RegExpUtils.Constants.REGX_USER_LOG_FILE_NAME_SEP1);
        if (split.length == 0) {
            return null;
        }
        final String userName = split[0];
        return userName;
    }

    public Date getDate() {
        return this.date;
    }

    public static Date getDate(final String userLogfileName) {
        final String dateTimeConn = LogFile.getDateTimeConn(userLogfileName);
        final String[] split = dateTimeConn.split(RegExpUtils.Constants.REGX_USER_LOG_FILE_NAME_SEP2);
        final String strDate = split[0];
        final String strTime = split[1];
        final SimpleDateFormat parser = new SimpleDateFormat("yyMMddHHmmss");
        final ParsePosition pos = new ParsePosition(0);
        pos.setIndex(0);
        final String dateTime = strDate + strTime;
        final Date date = parser.parse(dateTime, pos);
        return date;
    }

    public int getConnId() {
        return this.connId;
    }

    public static int getConnId(final String userLogfileName) {
        final String dateTimeConn = LogFile.getDateTimeConn(userLogfileName);
        final String[] split = dateTimeConn.split(RegExpUtils.Constants.REGX_USER_LOG_FILE_NAME_SEP2);
        final String strConnId = split[2];
        return Integer.parseInt(strConnId);
    }

    public static String getDateTimeConn(final String userLogfileName) {
        if (userLogfileName == null) {
            return null;
        }
        final String[] split = userLogfileName.split(RegExpUtils.Constants.REGX_USER_LOG_FILE_NAME_SEP1);
        if (split.length == 0) {
            return null;
        }
        final String dateTimeConn = split[1];
        return dateTimeConn;
    }

    public String getSessionId() {
        return this.sessionId;
    }

    public void setSessionId(final String sessionId) {
        this.sessionId = sessionId;
    }

    public List<LogQuery> getListLogQuery() {
        return this.lstLogQuery;
    }

    public List<LogTransaction> getListLogTransaction() {
        return this.lstLogTransaction;
    }

    @Override
    public String toString() {
        final StringBuilder sbString = new StringBuilder();
        sbString.append("-USER NAME[");
        sbString.append(this.getUserName());
        sbString.append("]");
        sbString.append(StringUtils.Constants.CRLF);
        sbString.append(".Transactions[");
        final List<LogTransaction> listLogTransaction = this.getListLogTransaction();
        sbString.append(listLogTransaction.size());
        sbString.append("]");
        for (final LogTransaction transaction : listLogTransaction) {
            sbString.append(StringUtils.Constants.CRLF);
            sbString.append("    -");
            sbString.append(transaction);
        }
        for (final LogQuery logQuery : this.getListLogQuery()) {
            if (logQuery.getTransaction() == null) {
                continue;
            }
            sbString.append(StringUtils.Constants.CRLF);
            sbString.append("    -");
            sbString.append(logQuery);
        }
        return sbString.toString();
    }

    public String getNode() {
        final File file = super.getFile();
        return LogFile.getParentFolder(file);
    }

    public static String getParentFolder(final File file) {
        final File absoluteFile = file.getAbsoluteFile();
        final File parentFile = absoluteFile.getParentFile();
        final String parent = parentFile.getName();
        return parent;
    }
}
